package com.sinodata.bsm.cicp.collector.net.trap;

import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.snmp4j.CommandResponderEvent;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.Variable;
import org.snmp4j.smi.VariableBinding;

import com.sinodata.bsm.cicp.cache.CacheFactory;
import com.sinodata.bsm.cicp.cache.ResCache;
import com.sinodata.bsm.cicp.collector.net.trap.impl.TrapProcess;

public class NetProcess extends TrapProcess {

    private ResCache resCache = (ResCache) CacheFactory.getCache(ResCache.class);
    //    private PDU pdu = new PDU();
    private EventSent eventSent = new EventSent();
    private static Logger logger = Logger.getLogger(NetProcess.class.getName());

    public boolean trapProcess(CommandResponderEvent respEvnt) {
        boolean flag = false;
        boolean b = true;
        Map map = new HashMap<OID, Variable>();
        Vector<VariableBinding> recVBs = respEvnt.getPDU().getVariableBindings();
        String ip = respEvnt.getPeerAddress().toString().split("/")[0];// 获取资源
        // 放入agent当中获取资源id

        for (int i = 0; i < recVBs.size(); i++) {
            VariableBinding recVB = recVBs.elementAt(i);
            OID oid = recVB.getOid();
            String variable = recVB.getVariable().toString();
            map.put(oid, variable);
        }
        String timeString = (String) map.get("1.3.6.1.2.1.1.3.0"); // 时间格式。
        String type = (String) map.get("1.3.6.1.6.3.1.1.4.1.0");
        Document document = DocumentHelper.createDocument();
        document.setXMLEncoding("gbk");
        Element root = document.addElement("request");
        Element eventElement = root.addElement("event");

        eventElement.addElement("res").addText("");// 根据ip获取资源
        eventElement.addElement("type").addText("321");// 事件类别

        if (type.equals("1.3.6.1.6.3.1.1.5.1")) {
            eventElement.addElement("msg").addText("设备冷启动");
            eventElement.addElement("level").addText("4");// 事件级别需讨论
        } else if (type.equals("1.3.6.1.6.3.1.1.5.2")) {
            eventElement.addElement("msg").addText("设备热启动");
            eventElement.addElement("level").addText("4");// 事件级别需讨论
        } else if (type.equals("1.3.6.1.6.3.1.1.5.3")) {
            String portString = (String) map.get("1.3.6.1.2.1.2.2.1.1");
            eventElement.addElement("msg").addText("端口:" + portString + " down");
            eventElement.addElement("level").addText("4");// 事件级别需讨论
        } else if (type.equals("1.3.6.1.6.3.1.1.5.4")) {
            String portString = (String) map.get("1.3.6.1.2.1.2.2.1.1");
            eventElement.addElement("msg").addText("端口:" + portString + " up");
            eventElement.addElement("level").addText("4");// 事件级别需讨论
        } else if (type.equals("1.3.6.1.6.3.1.1.5.5")) {
            eventElement.addElement("msg").addText("登陆认证失败");
            eventElement.addElement("level").addText("4");// 事件级别需讨论
        } else {
            b = false;
        }
        //        int resId = FactoryRegistry.getResourceFactory().getResource(ip).get(0).getResValue().getId();
        Long resId = resCache.getResByIp(ip).get(0).getId();
        eventElement.addElement("res").addText(resId + "");// 资源id
        eventElement.addElement("type").addText("700000"); // 定义好eventType之后写死在程序中
        eventElement.addElement("time").addText(timeString);
        eventElement.addElement("fault").addText("1");
        flag = true;
        if (b) {
            String str = document.asXML();
            str = str.replaceAll("\n", "");
            eventSent.sendEvent(str);
            logger.info("Trap:-------------->>" + str);
        }

        return flag;
    }
}
